package com.keyancompany.zhongbaopingtai.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;

import com.keyancompany.zhongbaopingtai.Constants;
import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.activity.DiscussAreaActivity;
import com.keyancompany.zhongbaopingtai.activity.DiscussDetailActivity;
import com.keyancompany.zhongbaopingtai.activity.TaskDetailActivity;
import com.keyancompany.zhongbaopingtai.adapter.DiscussAdapter;
import com.keyancompany.zhongbaopingtai.adapter.RefreshFootAdapter;

import com.keyancompany.zhongbaopingtai.model.TalkList;
import com.keyancompany.zhongbaopingtai.utils.IntentUtils;


import butterknife.Bind;

/**
 * 讨论区
 */
public class DiscussFragment extends BaseFragment {

    private static final String TYPE = "type";

    @Bind(R.id.demo_recycler)
    RecyclerView mRecycler;
    @Bind(R.id.demo_swiperefreshlayout)
    SwipeRefreshLayout mSwiperefreshlayout;
    private DiscussAdapter adapter;
    private LinearLayoutManager linearLayoutManager;
    private int lastVisibleItem;

    /**
     * Mandatory empty constructor for the fragment manager to instantiate the
     * fragment (e.g. upon screen orientation changes).
     */
    public DiscussFragment() {
    }

    public static DiscussFragment newInstance(int type) {
        DiscussFragment fragment = new DiscussFragment();
        Bundle args = new Bundle();
        args.putInt(TYPE, type);
        fragment.setArguments(args);
        return fragment;
    }

    private void initData() {

        if (((DiscussAreaActivity) getActivity()).getList() != null) {

            mRecycler.setAdapter(adapter = new DiscussAdapter(getActivity(), ((DiscussAreaActivity) getActivity()).getList()));

            adapter.setOnItemClickListener(new DiscussAdapter.OnRecyclerViewItemClickListener() {
                @Override
                public void onItemClick(View view, TalkList data) {
                    IntentUtils.startActivity(getActivity(), DiscussDetailActivity.class, Constants.TALK_CONTENT, data);
                }
            });

        }
        mSwiperefreshlayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                Log.d("TT", "invoke onRefresh...");
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
//                            List<String> newDatas = new ArrayList<String>();
//                            for (int i = 0; i < 5; i++) {
//                                int index = i + 1;
//                                newDatas.add("new item" + index);
//                            }
//                            adapter.addItem(newDatas);

//                            if(((DiscussAreaActivity)getActivity()).getList()!=null){
//                              adapter.addItem(((DiscussAreaActivity)getActivity()).getList());
//                            }

                        mSwiperefreshlayout.setRefreshing(false);
//                    T.showShort(getContext(),"更新了五条数据...");
                    }
                }, 5000);
            }
        });
        //RecyclerView滑动监听
        mRecycler.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 == adapter.getItemCount()) {
                    adapter.changeMoreStatus(RefreshFootAdapter.LOADING_MORE);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
//                                List<String> newDatas = new ArrayList<String>();
//                                for (int i = 0; i < 5; i++) {
//                                    int index = i + 1;
//                                    newDatas.add("more item" + index);
//                                }
//                              adapter.addMoreItem(((DiscussAreaActivity)getActivity()).getList());
//                              adapter.changeMoreStatus(RefreshFootAdapter.PULLUP_LOAD_MORE);
                        }
                    }, 2500);
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
            }
        });
    }


    @Override
    public int getContentViewId() {
        return R.layout.recycler_footview_layout;
    }

    @Override
    public void setUpViews() {

        //设置刷新时动画的颜色，可以设置4个
        mSwiperefreshlayout.setProgressBackgroundColorSchemeResource(android.R.color.white);
        mSwiperefreshlayout.setColorSchemeResources(android.R.color.holo_blue_light,
                android.R.color.holo_red_light, android.R.color.holo_orange_light,
                android.R.color.holo_green_light);
        mSwiperefreshlayout.setProgressViewOffset(false, 0, (int) TypedValue
                .applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources()
                        .getDisplayMetrics()));
        linearLayoutManager = new LinearLayoutManager(getActivity());//创建默认的线性LayoutManager
        linearLayoutManager.setOrientation(OrientationHelper.VERTICAL);
        mRecycler.setLayoutManager(linearLayoutManager);
        //添加分隔线
//          mRecyclerView.setHasFixedSize(true);//如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        initData();
    }

}
